package com.zhenhe.core.web.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhenhe.core.common.controller.BaseController;
import com.zhenhe.core.common.dao.Page;
import com.zhenhe.core.common.entity.RespJson;
import com.zhenhe.core.common.utils.framework.RespJsonFactory;
import com.zhenhe.core.common.utils.security.MD5;
import com.zhenhe.core.entity.TUserEntity;
import com.zhenhe.core.modules.service.TUserService;

@Controller
@RequestMapping("tusersetting")
public class TUserController extends BaseController{
	@Resource
	private TUserService tUserService;
	
	/**
	 * 管理员账号管理
	 * @return
	 */
	@RequestMapping("managersetting")
	public String managersetting(){
		return "back/system/managerSetting";
	}
	/**
	 * 领导账号管理
	 * @return
	 */
	@RequestMapping("leadersetting")
	public String leadersetting(){
		return "back/system/leaderSetting";
	}
	/**
	 * 办事员账号管理
	 * @return
	 */
	@RequestMapping("clerksetting")
	public String clerksetting(){
		return "back/system/clerkSetting";
	}
	/**
	 * 资料维护员账号管理
	 * @return
	 */
	@RequestMapping("maintainersetting")
	public String maintainersetting(){
		return "back/system/maintainerSetting";
	}
	
	/**
	 * 新增编辑页面
	 * @param model
	 * @param id 如果存在，则为编辑
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public TUserEntity edit(Integer id, ModelMap model){
		TUserEntity tUser = tUserService.get(id);
		tUser.setPassword(null);
		return tUser;
	}
	
	/**
	 * 查询及列表往页面传值
	 * @param titleLike
	 * @param keyWord
	 * @param isValid
	 * @param page
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Page<TUserEntity> list(String userName, String userAccount, String userRole, Page<TUserEntity> page){
		page.getSearch().put("userName", userName);
		page.getSearch().put("userAccount", userAccount);
		page.getSearch().put("userRole", userRole);
		tUserService.findList(page.getSearch());
		return page;
	}
	
	/**
	 * 新增编辑保存
	 * @param help
	 * @param result
	 * @return
	 */
	@RequestMapping(value="save", method={RequestMethod.POST})
	@ResponseBody
	public RespJson save(@Valid TUserEntity tUser, BindingResult result){
		if(result.hasErrors()){
			return RespJsonFactory.buildFailure(result.getAllErrors().get(0).getDefaultMessage(), "model.help.save.error");
		}
		if(tUser.getId()==null){
			//判断用户是否存在
			if(tUserService.isUserExist(tUser)){
				return RespJsonFactory.buildFailure("module.userId.exist");
			}
		}
		if(tUser.getPassword()!=null){
			tUser.setPassword(MD5.getMD5(tUser.getPassword()));
		}
		tUserService.save(tUser);
		return RespJsonFactory.buildSuccess();
	} 
	
	/**
	 * 删除
	 * @param tUser
	 * @return
	 */
	@RequestMapping(value="delete", method={RequestMethod.DELETE})
	@ResponseBody
	public RespJson delete(@RequestBody TUserEntity tUser){
		tUserService.delete(tUser.getId());
		return RespJsonFactory.buildSuccess();
	}
}
